public without sharing class  EquipmentIntroduceController {

    public WorkOrder__c workOrder{get;set;}

    public Id id{get;set;}
    
    //结果及建议
    public String resSugestion {get;set;}
    //服务情况
    public List<String> serviceSituation {get;set;}
    //仪器名称
    public String deviceName {get;set;}
    //仪器编号
    public String deviceNo {get;set;}
    //开始时间
    public String startTime {get;set;}
    //结束时间
    public String endTime {get;set;}
    //科室负责人
    public String responsePerson {get;set;}
    //科室负责人电话
    public String responsePhone {get;set;}
    ///顾客名称
    public String accName {get;set;}
      ///不良事件描述
    public String badDescribe{get;private set;}

    public Boolean isBad {get; private set;}

    public String feedbackNote {get;private set;}
    //回访时间
    public String feadBackTime{get;private set;}
    //客户签名
    public Attachment accountSign {get;set;}
    //参训数据 
    public String persons {get; private set;}
    
    public Boolean isMQGEN {get; private set;}
    
  
    public EquipmentIntroduceController(ApexPages.StandardController con) {
        id=con.getId();
        workOrder = [  SELECT 
                    //工单
                     w.Name,w.Id,
                     //科室负责人
                     w.ManagementDepContact__r.Name,w.ManagementDepContact__r.MobilePhone,
                     //联系人
                     w.Contact__r.Name,w.Contact__r.MobilePhone,w.ReturninContact__c   ,
                     //客户
                     w.Account__c,w.Account__r.Name,w.Account__r.Province__r.Name,
                     //产品
                     w.ProductType2__c,w.ServiceManufacture__r.Product__r.Name ,w.ServiceManufacture__r.IMEINumber__c, 
                     //结果
                     w.ResultsSuggestions__c,w.ServiceSituation__c,Participant__c,
                     //服务类型
                     w.RecordType.Name,w.ServiceType__c,w.ServiceTrackingNotes__c,
                     //情况描述
                     w.Reason_Desc__c,w.AdverseEventDescription__c,AdverseEvent__c,
                     //时间
                     w.Install_Start_Datetime__c,w.Install_End_Datetime__c,w.ServicesTrackingTime__c,
                     //创建人
                     w.CreatedBy.Name ,w.CreatedBy.MobilePhone   
                            
                        FROM WorkOrder__c w 
                        WHERE w.Id = :id ];
                        
                        
        if(null!=workOrder){
        
           /* List<Contact> contact= [  SELECT Name,MobilePhone 
                                            FROM Contact c
                                            WHERE c.Account.Id = : workOrder.Account__c and 
                                            c.Departments__c = '检验科' and c.Post__c = '科长/主任' and
                                            c.PositionSituation__c = '在职'
                                             ];
            for(Contact c : contact){
                 responsePerson = c.Name;
                 responsePhone = c.MobilePhone;
                 break;    
             }*/
List<Attachment> attachment= [select Body,Name,CreatedDate,Id from Attachment where ParentId = :id];

for(Attachment att : attachment){
if (att.Name=='使用科室负责人_Sign.jpg'){
accountSign=att;
}else if( att.Name=='管理科室负责人_Sign.jpg'){
//manageSign=att;
}else if( att.Name == '工程师_Sign.jpg'){
//engineerSign = att;
}
}          
            responsePerson = workOrder.ManagementDepContact__r.Name;
           responsePhone = workOrder.ManagementDepContact__r.MobilePhone;
           isMQGEN = System.currentPageReference().getUrl().toUpperCase().contains('MAQEN') ;
           Integer len = System.currentPageReference().getUrl().toUpperCase().contains('MAQEN') ? 58 : 50;
           
           
            persons = kaigyo(workOrder.Participant__c, 58);
            if(workOrder.Install_Start_Datetime__c != null){
                startTime = workOrder.Install_Start_Datetime__c.format('yyyy年MM月dd日');
            }
            if(workOrder.Install_End_Datetime__c != null){
                endTime = workOrder.Install_End_Datetime__c.format('yyyy年MM月dd日');
            }
            resSugestion = kaigyo(workOrder.ResultsSuggestions__c , len);
            serviceSituation = autoNextLine(workOrder.ServiceSituation__c , len);
            //deviceName = kaigyo('测试文本测试文本测试文本文本测试文本', 9);

            deviceNo = kaigyo(workOrder.ServiceManufacture__r.IMEINumber__c, 9);
            accName =  kaigyo(workOrder.Account__r.Name, 17);
           // resSugestion = kaigyo(workOrder.ResultsSuggestions__c , 50);
           // serviceSituation  =  autoNextLine(workOrder.ServiceSituation__c , 50);
            deviceName = kaigyo(workOrder.ServiceManufacture__r.Product__r.Name ,50);
            badDescribe =  kaigyo(workOrder.AdverseEventDescription__c,50);
            accName = kaigyo(workOrder.Account__r.Name, 50);
            feedbackNote = kaigyo(workOrder.ServiceTrackingNotes__c, 58);
            isBad = workOrder.AdverseEvent__c == '有不良事件' ? true : false ;
            feadBackTime = workOrder.ServicesTrackingTime__c != null ? workOrder.ServicesTrackingTime__c.format('yyyy年MM月dd日'):'';
 

        }
    }
      public void init(){
        WorkOrder__c wo = new WorkOrder__c(
            Id = workOrder.Id,
            GenerateTechnicalServiceReportTime__c = System.now()
        );

       update wo;
    }
    private List<String> autoNextLine(String value, Integer posLength ){
    	  List <String> temp = new List<String>();
      if(value == null) 
      {
          temp.add(value);
          return temp; 
      }
         List<String> str = new List<String>();
          str =  value.split('\r\n',-1);
          for (String s :str){

              if(s.length() > posLength){
                 for(Integer i = 0; i < s.length(); i++){
            		 if(Math.mod(i, posLength ) == 0){
                     	if(s.length() > i + posLength){
                    		 temp.add(s.substring(i, i + posLength)); 
                		 }else{ 
                     		temp.add(s.substring(i,s.length()));
                 		 } 
                 
                // kaigyoCount[col]++;
               		 }
         		 }
              
              }else{
                  temp.add(s);
              
              }
          }
         
          return temp;
    }
    //计算文本长度自动换行 value字符长度 posLength换行字数
    private String kaigyo(String value, Integer posLength ){
       if(value != null) value = value.replaceAll('\r\n','');
       if(value == null || value.length() <= posLength) return value;
         
         String rtnValue = value.substring(0, posLength ) + '\n';
          
         for(Integer i = posLength; i < value.length(); i++){
             if(Math.mod(i, posLength ) == 0){
                 if(value.length() > i + posLength){
                     rtnValue += value.substring(i, i + posLength ) + '\n'; 
                 }else{ 
                     rtnValue += value.substring(i,value.length());
                  } 
                // kaigyoCount[col]++;
               }
          }
             return rtnValue;
     }
     
    
}